<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
    <title>权限配置</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta name="author" content="飞啊飘啊">
	<meta name="hashcode" content="-2096005416">
	
	<script>window.duteTag="lz";</script>
	<script type="text/javascript" src="/admin/js/dute-1.0.js"></script>
	<script type="text/javascript" src="/admin/js/jquery-1.7.1.min.js"></script>
	
	<script type="text/javascript" src="/admin/js/lazy3q.ui.js"></script>
	<script>
		document.write('<link rel="stylesheet" type="text/css" href="/admin//css/'+($.cookie('theme')||'elegant')+'/lazy3q.ui.css" /> ');
	</script>
	
	
	<!-- 用户自定义js(懒加载) -->
	<script type="text/javascript" src="/admin/js/define.js"></script>
	<!-- 用户自定义css样式 -->
	<link rel="stylesheet" type="text/css" href="/admin/css/define.css"/>
	<!-- icon font -->
  	<link rel="stylesheet" href="/admin/css/iconfont.css">
	
	<script type="text/javascript">
		window.basePath="/";
		window.contextPath="/admin/";
		window.canQuery=true;
		window.className = "com.distribute.framework.model.SrvLimited";
		window.modelPath = "/admin/permissions/limit";
		window.hashcode = "-2096005416";
	</script>
		
	<!-- 感谢My97DatePicker出品的时间控件 -->
	<script language="javascript" type="text/javascript" src="/admin/js/My97DatePicker/WdatePicker.js"></script>
	
	<!------------------------VIEW_HTML_HEAD------------------------>
	<!------------------------VIEW_HTML_HEAD------------------------>	
	
</head>
<script>
Page({
  data:{
    
  },
  onLoad: function(options) {
  	var THIS = this;
  	$.getAjax("limit.list.do",options,function(protocol){
  		window.protocol = protocol;
  		if(protocol.message){
  			$.toast(protocol.message);
  		}
  		THIS.setData(protocol.data);
  		/******************VIEW_HTML_SET_DATA******************/
  		/******************VIEW_HTML_SET_DATA******************/
  	});
  }
});
</script>
<body style="display:none;">
<!-- [apt:body] -->
<div id="user_grid" style="left:0px; top:0px; width:30%; bottom:50px; position:absolute;overflow-y:scroll;">
	<table id="limit-table" class="table" width="100%" border="0" cellspacing="1" cellpadding="0">
		<thead class="state-header">
		  	<tr>
		  		<td>系统角色</td>
		  		<td width="120">操作</td>
		  	</tr>
		</thead>
		<tbody>
			<tr lz:for="groups" id="g{{item.sgId}}" tree="g{{item.sgParent}}">
				<td class="node">{{item.sgName}}</td>
		  		<td><a href="?group={{item.id}}">设置权限</a></td>
			</tr>
		</tbody>
	</table>
</div>
<div style="left:0px; height:50px; width:30%; bottom:0px; position:absolute;">
	<div style="margin:3px;">
		&nbsp;修改系统角色的权限时，将影响被修改权限的系统角色下级所有的用户的权限。
	</div>
</div>

<div style=" width:70%; top:0px; bottom:0px; right:0px; position:absolute;">
	<div id="limit" style="left:0px; top:0px; width:100%;bottom:50px;position:absolute;overflow-y:scroll;"></div>
	<div style="left:0px; height:50px; bottom:0px; width:100%;position:absolute;">
		<div style="margin:3px;">
			<input type="hidden" name="group" id="group" value="{{group}}" />
			<input type="hidden" name="limit_list" id="limit_list" value="" />
			<button type="button" class="button ui-state-default" onclick="SubmitLimit()" style="margin:10px; float:right;">保存修改</button>
			</form>
			<button class="button" onclick="ClearLimit()" style="margin:10px; float:right;">清空权限</button>
			<button class="button" onclick="SelectAll()"  style="margin:10px; float:right;">全&nbsp;&nbsp;选</button>
			<div style="margin-left:10px;line-height:50px; float:left;">
				<strong>当前系统角色：</strong>
				{{srvGroup.sgName}}
			</div>
		</div>
	</div>
</div>
<script>
$(function(){
	$("#limit-table").treegrid({expand:true});
	$("#limit-table").find(".icon").click();
	$("body").attr("scroll","no").css("overflow","hidden");
	$.resize(function(){
		$("#user_grid").css("height",($(window).height()-50)+"px");
		$("#limit_frame").css("height",$(window).height()+"px");
		$("#user_grid").css("width",$(window).width()*3/10+"px");
		$("#limit_frame").css("width",$(window).width()*7/10+"px");
	});
});
</script>

<script>
var bytes=[128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216];
$(function(){
 	initLimit(protocol.data);
});
function initLimit(data){
	var limitGrid=$("#limit").grid({
		column:[["项目名称",150],["查看",40],["增加",40],["修改",40],["删除",40],["导入",40],["导出",40],["分析",40],["&nbsp;&nbsp;",500]],
		height:$(window).height()-50
	});
	window.projects=[];
	for(var o in data.projects){
		var item = data.projects[o];
		projects[""+item.spId]=(projects[""+item.spParentId]?projects[""+item.spParentId]:limitGrid).add({data:["<a onclick='onCheck("+item.id+")' href='javascript:void(0);'>"+item.spName+"</a>","","","","","",""],expand:true});
		projects[""+item.spId].limitExp=item.spLimitExp;
		projects[""+item.spId].exps=[];
	}
	
	window.limits=[];
	for(var o in data.limits){
		var item = data.limits[o];
		limits[""+item.slProject]=item.slLimit;
	}
	
	/*****************根据设置者的权限创建checkbox******************/
	for(var o in projects)
	{
		var bDisabled;
		
		bDisabled=( (limits[o] && ((limits[o] & 1)==1)) || data.EXTEND_BY_SUPER);
		var can_view=projects[o].can_view=$("<input onclick='OnCheck(this)' type='checkbox' id='view_"+o+"' name='view_"+o+"' value='1' "+(bDisabled?"":"disabled")+"/>").get(0);
		can_view.project=projects[o];
		projects[o].tds[1].get(0).appendChild(can_view);
		
		bDisabled=( (limits[o] && ((limits[o] & 2)==2)) || data.EXTEND_BY_SUPER);	
		var can_add=projects[o].can_add=$("<input onclick='OnCheck(this)' type='checkbox' id='add_"+o+"' name='add_"+o+"' value='2' "+(bDisabled?"":"disabled")+"/>").get(0);
		can_add.project=projects[o];
		projects[o].tds[2].get(0).appendChild(can_add);
		
		bDisabled=( (limits[o] && ((limits[o] & 4)==4)) || data.EXTEND_BY_SUPER);	
		var can_edit=projects[o].can_edit=$("<input onclick='OnCheck(this)' type='checkbox' id='edit_"+o+"' name='edit_"+o+"' value='4' "+(bDisabled?"":"disabled")+"/>").get(0);
		can_edit.project=projects[o];
		projects[o].tds[3].get(0).appendChild(can_edit);
		
		bDisabled=( (limits[o] && ((limits[o] & 8)==8)) || data.EXTEND_BY_SUPER);
		var can_del=projects[o].can_del=$("<input onclick='OnCheck(this)' type='checkbox' id='del_"+o+"' name='del_"+o+"' value='8' "+(bDisabled?"":"disabled")+"/>").get(0);
		can_del.project=projects[o];
		projects[o].tds[4].get(0).appendChild(can_del);
		
		bDisabled=( (limits[o] && ((limits[o] & 16)==16)) || data.EXTEND_BY_SUPER);
		var can_import=projects[o].can_import=$("<input onclick='OnCheck(this)' type='checkbox' id='import_"+o+"' name='import_"+o+"' value='16' "+(bDisabled?"":"disabled")+"/>").get(0);
		can_import.project=projects[o];
		projects[o].tds[5].get(0).appendChild(can_import);
		
		bDisabled=( (limits[o] && ((limits[o] & 32)==32)) || data.EXTEND_BY_SUPER);
		var can_export=projects[o].can_export=$("<input onclick='OnCheck(this)' type='checkbox' id='export_"+o+"' name='export_"+o+"' value='32' "+(bDisabled?"":"disabled")+"/>").get(0);
		can_export.project=projects[o];
		projects[o].tds[6].get(0).appendChild(can_export);
		
		bDisabled=( (limits[o] && ((limits[o] & 64)==64)) || data.EXTEND_BY_SUPER);
		var can_stats=projects[o].can_stats=$("<input onclick='OnCheck(this)' type='checkbox' id='stats_"+o+"' name='stats_"+o+"' value='64' "+(bDisabled?"":"disabled")+"/>").get(0);
		can_stats.project=projects[o];
		projects[o].tds[7].get(0).appendChild(can_stats);
		
		
		if(projects[o].limitExp){
			projects[o].exps=[];
			var array = projects[o].limitExp.split(",");
			for(var i=0;i<array.length;i++){
				bDisabled=( (limits[o] && ((limits[o] & bytes[i])==bytes[i])) || data.EXTEND_BY_SUPER);
				var can_exp=projects[o].exps[projects[o].exps.length]=$("<input onclick='OnCheck(this)' type='checkbox' id='exp_"+o+"_"+i+"' name='exp_"+o+"_"+i+"' value='"+bytes[i]+"' "+(bDisabled?"":"disabled")+"/>").get(0);
				can_exp.project=projects[o];
				if(i==8)projects[o].tds[8].get(0).appendChild($("<br/>").get(0));
				projects[o].tds[8].get(0).appendChild(can_exp);
				projects[o].tds[8].get(0).appendChild($("<span>"+array[i]+"</span>").get(0));
			}
		}
	}
	
	
	/*****************根据被设置者的权限加载权限******************/
	var limiteds=new Array();
	var project;
	for(var o in data.limiteds){
		var limited = data.limiteds[o];
		project=projects[""+limited.slProject];
		if(project)
		{
			project.can_view.checked=(limited.slLimit & 1) ==1;
			project.can_add.checked=(limited.slLimit & 2) ==2;
			project.can_edit.checked=(limited.slLimit & 4) ==4;
			project.can_del.checked=(limited.slLimit & 8) ==8;
			project.can_import.checked=(limited.slLimit & 16) ==16;
			project.can_export.checked=(limited.slLimit & 32) ==32;
			project.can_stats.checked=(limited.slLimit & 64) ==64;
			for(var i=0;i<bytes.length;i++){
				if(project.exps[i])
					project.exps[i].checked=(limited.slLimit & bytes[i]) ==bytes[i];
			}
			project.limitId=limited.slId;
		}
	}
}


function onCheck(pid){
	checkAll(projects[pid]);
}
function checkAll(project){
	if(!project)
		return;
	project.can_view.checked=true;
	project.can_add.checked=true;	
	project.can_edit.checked=true;
	project.can_del.checked=true;
	project.can_import.checked=true;
	project.can_export.checked=true;
	project.can_stats.checked=true;
	for(var i=0;i<bytes.length;i++){
		if(project.exps[i])
			project.exps[i].checked=true;
	}
	for(var o in project.childs)
		checkAll(project.childs[o]);
}
function OnCheck(check)
{		
	if(check.checked){//如果当前被选中，则查看权限也应该被选中，级连所有父节点的查看权限也被选中
		var project=check.project;
		while(project){
			project.can_view.checked=true;
			project=project.onwer;
		}
	}
	CheckAll(check.project,check.checked,check.value);	
}
//级连所有子节点的所有权限都不被选中
function CheckAll(project,bChecked,value)
{
	if(!project)
		return;
	if(!bChecked && value==1){
		project.can_view.checked=bChecked;
		project.can_add.checked=bChecked;	
		project.can_edit.checked=bChecked;
		project.can_del.checked=bChecked;
		project.can_import.checked=bChecked;
		project.can_export.checked=bChecked;
		project.can_stats.checked=bChecked;
		for(var i=0;i<bytes.length;i++){
			if(project.exps[i])
				project.exps[i].checked=bChecked;
		}
		for(var o in project.childs)
			CheckAll(project.childs[o],bChecked,value);
	}else{
		if(value==1 || bChecked)project.can_view.checked=bChecked;
		if(value==2)project.can_add.checked=bChecked;	
		if(value==4)project.can_edit.checked=bChecked;
		if(value==8)project.can_del.checked=bChecked;
		if(value==16)project.can_import.checked=bChecked;
		if(value==32)project.can_export.checked=bChecked;
		if(value==64)project.can_stats.checked=bChecked;
		for(var o in project.childs)
			CheckAll(project.childs[o],bChecked,value);
	}
}




//清空
function ClearLimit()
{
	for(var o in projects)
	{
		var project=projects[o];
		project.can_view.checked=false;
		project.can_add.checked=false;
		project.can_del.checked=false;
		project.can_edit.checked=false;
		project.can_import.checked=false;
		project.can_export.checked=false;
		project.can_stats.checked=false;
		for(var i=0;i<bytes.length;i++){
			if(project.exps[i])
				project.exps[i].checked=false;
		}
	}	
}

//全选
function SelectAll()
{
	for(var o in projects)
	{
		var project=projects[o];
		if(!project.can_view.disabled)
			project.can_view.checked=true;	
		if(!project.can_add.disabled)
			project.can_add.checked=true;
		if(!project.can_del.disabled)
			project.can_del.checked=true;
		if(!project.can_edit.disabled)
			project.can_edit.checked=true;
		if(!project.can_import.disabled)
			project.can_import.checked=true;
		if(!project.can_export.disabled)
			project.can_export.checked=true;
		if(!project.can_stats.disabled)
			project.can_stats.checked=true;
		for(var i=0;i<bytes.length;i++){
			if(project.exps[i] && !project.exps[i].disabled)
				project.exps[i].checked=true;
		}
	}
}

//提交权限
function SubmitLimit()
{
	var strLimitList="";
	for(var o in projects)
	{
		if(strLimitList!="")
			strLimitList+=",";
			
		var project=projects[o];		

		if(project.limitId)
			strLimitList+=project.limitId;
		else
			strLimitList+="0";
		strLimitList+="|";
		strLimitList+=o;
		strLimitList+="|";
		
		var limitValue=0;
		if(project.can_view.checked)
			limitValue|=1;
		if(project.can_add.checked)
			limitValue|=2;
		if(project.can_edit.checked)
			limitValue|=4;
		if(project.can_del.checked)
			limitValue|=8;
		if(project.can_import.checked)
			limitValue|=16;
		if(project.can_export.checked)
			limitValue|=32;
		if(project.can_stats.checked)
			limitValue|=64;
			
		for(var i=0;i<bytes.length;i++){
			if(project.exps[i] && project.exps[i].checked)
				limitValue|=bytes[i];
		}
		
		strLimitList+=limitValue;
	}
	
	
	$.getAjax("limit.submit.do",{
		limit_list:strLimitList,
		group:$("#group").val()
	},function(protocol){
  		if(protocol.message){
  			alert(protocol.message);
  		}
  		window.location.href=window.location.href;
  	});
}
</script> 

<!-- [apt:body] -->
</body>
<script>

	window.navigate = top.$.open;
	window.href = function(url){
	   window.navigate(url,{edittype:"detail"},window);
	}


	$(function(){
		//修改权限配置任意字段
		$(document).on("dblclick",".table tbody td.td",function(){
			if((""+window.protocol.data.UID)!="0")return;
			var url = "/admin/permissions/limit.edit.html";
			var params = {id:$(this).parents("tr:eq(0)").attr("id"),edittype:"any",fields:$(this).attr("ref")};
			top.$.open(url,params,window);
		});
		$(".toolbar .batch dd a").click(function(){
			return false;
		});
		//批量修改任意字段
		$(".toolbar .batch dd").click(function(){
			if((""+window.protocol.data.UID)!="0")return;
			var ids = "";
			$(".check:checked").each(function(i){
				if(ids!="")ids+=",";
				ids+=$(this).val();
			});
			var struct = $.getObject("/admin/struct.do",{className:"com.distribute.framework.model.SrvLimited",struct:true});
			var strHtml = "";
			for(var o in struct){
				if("slId".indexOf(struct[o].name)>-1)continue;
				strHtml+="<label class='syllable'><input value='"+struct[o].name+"' type='checkbox'/>"+struct[o].caption+"&nbsp;&nbsp;</label>";
			}
			var div = $("<div style='width:"+($(window).width()/2)+"px;'><div style='padding:5px;' class='highlight'></div><div>"+strHtml+"</div></div>");
			if(ids!=""){
				div.find(".highlight").html("当前选中了"+(ids.split(",").length)+"条要修改的数据");
			}else{
				div.find(".highlight").html("当前将批量修改"+protocol.data.pageInfo.count+"条查询到的数据");
			}
			var dialog = div.dialog({
				title:"请选择要批量修改字段",modal:true,
				onOk:function(){
					if(div.find("input:checked").size()==0){
						$.toast("未选中任何要批量修改的字段");
						return false;
					}
					var url = "/admin/permissions/limit.edit.html";
					var params = {edittype:"any"};
					if(ids!=""){
						params["ids"]=ids;
					}else{
						params["PARAMS"]=encodeURI(JSON.stringify(protocol.data.PARAMS));
					}
					var strFields = "";
					div.find("input:checked").each(function(){
						if(!strFields=="")strFields+=",";
						strFields+=$(this).val();
					});
					params.fields = strFields;
					top.$.open(url,params,window);
				}
			});
		});
	});

	$(function(){
		if(parent.setit){
			parent.setit("/admin/permissions/limit.html","权限配置","/admin//images/icons/2008112220121595877803.png");
		}
	});
	/**
	* 删除权限配置
	**/
	function deleteLimit(id,flag)
	{
		var dialog=top.$.dialog({
				html:"<span style='white-space:nowrap;'>确定要删除权限配置["+flag+"]吗？</span>",
				title:"删除权限配置",
				modal: true,			
				onOk:function(){
					dialog.closeDialog();
					var url = "/admin/permissions/limit.delete.do";
					var params = {id:id};
					var protocol = $.getAjax(url,params);
					if(protocol.message){
						top.$.toast(protocol.message);
					}
					if(protocol.success){
						window.location.href=window.location.href;		
					}
				}
		 });	
	}
	/*
	* 删除所选记录详情
	*/
	function deletes(){
		var checker=$(".check:checked");
		if(checker.length==0){
			$.tips("您未选中任何记录，请至少选择一项.");
			return;
		}
		var dialog=top.$.dialog({
				html:"<span style='white-space:nowrap;'>确定要删除所选权限配置吗？</span>",
				title:"批量删除权限配置",
				modal: true,			
				onOk:function(){
					var ids = "";
					checker.each(function(i){
						if(ids!="")
							ids+=",";
						ids+=$(this).val();
					});
					dialog.closeDialog();
					var url = "/admin/permissions/limit.delete.do";
					var params = {ids:ids};
					var protocol = $.getAjax(url,params);
					if(protocol.message){
						top.$.toast(protocol.message);
					}
					if(protocol.success){
						window.location.href=window.location.href;		
					}
				}
		});
	}
</script>
</html>